<script setup lang="ts">
import { onMounted, ref, computed } from "vue";

import { useExamStore } from "@/store/modules/exam";
const examStore = useExamStore();

const props = withDefaults(
    defineProps<{
        question: any; // 题目
        isright: boolean; // 是否正确
        isResult: boolean; // 是否是结果页
    }>(),
    {
        isright: false,
        isResult: false,
    }
);

const emit = defineEmits(["writeAnswer"]);


onMounted(() => {


})
const isMobile = computed(() => {
    const viewportWidth = document.documentElement.clientWidth;
    return viewportWidth <= 768;
});

const questionId = ref(0);

const showPicker = ref(false);

//@ts-ignore
const confirm = ({ selectedValue, selectedOptions }) => {
    examStore.saveChildrenUserAnswer(selectedValue[0], questionId.value);
}

const selectChange = () => {
    examStore.setProgress();
}
</script>

<template>
    <div class="question">
        <div class="top-box">
            <div>
                <div class="title">{{ props.question.title }}</div>
                <div class="describe" v-html="props.question.describe"></div>
            </div>

        </div>
        <div class="question-content">
            <div class="left-img">
                <div>Supporting information</div>
                <div v-for="item in props.question.questionList[0].options">
                    <span v-html="item.opNumber" style="margin-right: 10px;"></span>
                    <span v-html="item.opContent" style="font-weight: 400;"></span>
                </div>
            </div>
            <div class="right-content">
                <div v-if="isMobile">
                    <div class="single-row" v-for="(item) in $props.question.questionList"
                        @click="() => { showPicker = true; questionId = item.id; }">
                        <div v-html="item.content"></div>

                        <div class="answer">
                            {{ item.userAnswer }}
                        </div>
                    </div>
                    <nut-picker v-model:visible="showPicker" :columns="$props.question.questionList[0].options.map((itemOp: any) => {
                    return {
                        text: itemOp.opNumber,
                        value: itemOp.opNumber
                    }
                })" title="选择" @confirm="confirm">
                    </nut-picker>
                </div>
                <div v-if="!isMobile">
                    <div class="single-content" v-for="(item) in $props.question.questionList">
                        <div v-html="item.content"></div>
                        <el-select v-model="item.userAnswer" placeholder="请选择答案" style="width: 240px"
                            @change="selectChange">
                            <el-option v-for="itemOption in item.options.map((itemOp: any) => {
                    return {
                        text: itemOp.opNumber,
                        value: itemOp.opNumber
                    }
                })" :key="itemOption.value" :label="itemOption.text" :value="itemOption.value" />
                        </el-select>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.question {

    .top-box {
        display: flex;
        align-items: center;
        justify-content: space-between;
        border-bottom: 3px solid #F7F7F7;


        .audio-box {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .progress {
            margin-bottom: 0;
            width: 340px;
        }
    }

    .title {
        font-size: 24px;
        color: #141623;
        line-height: 22px;
        margin-bottom: 15px;

        &::before {
            content: "";
            display: inline-block;
            width: 6px;
            height: 14px;
            border-radius: 3px;
            background-color: #00CCCF;
            margin-right: 12px;
            margin-top: 2px;
        }
    }

    .describe {
        font-size: 24px;
        color: #14161E;
        line-height: 30px;
        padding-bottom: 30px;
        font-weight: 400;
    }

    .question-content {
        display: flex;
        justify-content: space-between;
        padding-top: 20px;

        .left-img {
            width: 45%;
            color: #000;

            img {
                width: 100%;
            }
        }

        .right-content {
            width: 50%;
        }

        .single-content {
            display: flex;
            align-items: center;
            margin-bottom: 20px;
            font-weight: 400;
            div {
                margin-right: 20px;
            }
        }
    }
}

@media (max-width: 768px) {
    .question {
        position: relative;

        .title {
            font-size: 14px;
            color: #141623;
            line-height: 18px;
        }

        .title::before {
            content: none;
        }

        .describe {
            font-weight: 400;
            font-size: 14px;
            color: #9395A4;
            line-height: 18px;
            border-bottom: none;
            padding-bottom: 0;
        }

        .question-title {
            font-weight: 500;
            font-size: 16px;
            color: #141623;
            line-height: 24px;
        }

        .audio-box {
            position: absolute;
            right: 0;
            top: 0;
        }

        .question-content {
            flex-direction: column;
            gap: 20px;

            .left-img,
            .right-content {
                width: 100%;
            }

            .right-content {
                .single-row {
                    display: flex;
                    gap: 20px;
                    margin-bottom: 20px;
                    align-items: center;
                    font-weight: 400;
                }

                .answer {
                    border-bottom: 1px solid #000;
                    min-width: 50px;
                    text-align: center;
                    height: 23px;
                }
            }
        }
    }
}
</style>

<style>
.icon_voice_play {
    width: 20px;
}
</style>
